﻿Shader "CookbookShaders/AnimatingSprite"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_TexWidth ("Sheet Width", float) = 0.0
		_CellAmount ("Cell Amount", float) = 0.0
		_Speed ("Speed", Range(0.01, 32)) = 12
	}
	SubShader{
		Tags { "RenderType"="Opaque" }
		LOD 100

		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		float _TexWidth;
		float _CellAmount;
		float _Speed;

		struct Input {
			fixed2 uv_MainTex;
		};

		void surf(Input IN, inout SurfaceOutput o) {
			float2 spriteUV = IN.uv_MainTex;
			float perFrameAdd = 1 / _CellAmount;

			float timeVal = fmod(_Time.y * _Speed, _CellAmount);
			timeVal = ceil(timeVal);
			
			float xValue = spriteUV.x;
			xValue *= perFrameAdd;
			xValue +=  timeVal * perFrameAdd;
			
			spriteUV = fixed2(xValue, spriteUV.y);
			half4 c = tex2D(_MainTex, spriteUV);
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
}